package game.bejeweled
{
	import com.yzy.models.SetLibs;
	
	import flash.geom.Point;
	
	import effect.Effect;
	
	
	import models.BallModel;
	import models.ProfileModel;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.Sprite;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.textures.Texture;

	public class BejeweledBlock extends Sprite
	{
//		private var createBall:CreateBalls;
		public var backdropTexture:Texture
		private var turnBalls:Vector.<BackDrop>=new Vector.<BackDrop>();
	
		public function BejeweledBlock()
		{
		}
		
		public function init(xml:XMLList):void{
			var balls:XMLList=xml.balls[0].ball;
			ProfileModel.instant.row=int(xml.@row);
			ProfileModel.instant.column=int(xml.@column);
			ProfileModel.instant.EvenSum=int(xml.EvenSum);
			this.x=int(xml.@x);
			this.y=int(xml.@y);
			var ballsTotal:int=balls.length();
			for (var i:int = 0; i < ballsTotal; i++) 
			{
				var ballmodel:BallModel=new BallModel();
				ballmodel.ballName=balls[i].@image;
				ballmodel.degree=balls[i].@degree;
				ballmodel.property=balls[i].@property;
				ballmodel.ballTexture=Texture.fromBitmapData(SetLibs.getBitmapData(ballmodel.ballName));
				ProfileModel.instant.ballModelVec[i]=ballmodel;
			}
			
			//
			backdropTexture=Texture.fromBitmapData(SetLibs.getBitmapData(xml.balls[0].@backdrop));
			
			//control
			this.addEventListener(TouchEvent.TOUCH,onClick);
			//
//			runGame();
		}
		
		private function runGame():void{
//			createBall.addEventListener(ControlEvent.CREATE_BALL_COMPLETE,function sameball():void{createBall.sameBall()});
//			createBall.addEventListener(ControlEvent.REMOVE_BALL_COMPLETE,function createball():void{createBall.createBall()});
//			
//			createBall.createBall();
			
			
		}
		private var tween:Tween;
		private function onClick(e:TouchEvent):void{
			if(!ProfileModel.instant.AllowCtrl)return;
			var touches:Vector.<Touch>=e.getTouches(this);
			
			if(touches.length==1){
				var touch:Touch=touches[0];
				if(touch.phase==TouchPhase.ENDED){
					var clicBall:Ball=e.target as Ball;
					if(clicBall==null)return;
					
					if(TurnBall(ProfileModel.instant.backdropVec[clicBall.rowNum][clicBall.column])){
						
						ProfileModel.instant.AllowCtrl=false;
						var tempBall:Ball=turnBalls[0].currBall;
						turnBalls[0].currBall=turnBalls[1].currBall;
						turnBalls[1].currBall=tempBall;
						
						Effect.instant.moveBallEffect(turnBalls[0].currBall,new Point(turnBalls[0].ballX,turnBalls[0].ballY));
						Effect.instant.moveBallEffect(turnBalls[1].currBall,new Point(turnBalls[1].ballX,turnBalls[1].ballY));
						turnBalls=new Vector.<BackDrop>();	
					}else{
						//clicBall 闪
						if(turnBalls.length>1){
							turnBalls.shift()
						};
					}
				}
			}
		}
		
		private function TurnBall(backdrop:BackDrop):Boolean{
			var isTurn:Boolean=false;
			turnBalls.push(backdrop);
			if(turnBalls.length>1){
				if(Math.abs(turnBalls[0].rowNum-backdrop.rowNum)<=1 && Math.abs(turnBalls[0].column-backdrop.column)<=1){
					isTurn=true;
					Starling.juggler.removeTweens(tween.target);
					tween.target.alpha=1;
					tween.target.scaleX=1;
					tween.target.scaleY=1;
				}else{
					Starling.juggler.removeTweens(tween.target);
					tween.target.alpha=1;
					tween.target.scaleX=1;
					tween.target.scaleY=1;
					isTurn=false;
					tween=Effect.instant.lightning(backdrop.currBall);
				}
			}else{
				tween=Effect.instant.lightning(backdrop.currBall);
				isTurn=false;				
			}
			return isTurn;
		}
		//*****************************************************
		// Singleton 
		//*****************************************************
		private static var _instant:BejeweledBlock;
		public static function get instant():BejeweledBlock
		{
			if(null == _instant)
			{
				_instant =  new BejeweledBlock();
			}
			return _instant;
		}
	}
}